package com.baidu.health.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baidu.health.pojo.TbSensitivity;
import com.baidu.health.mapper.TbSensitivityMapper;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class TbSensitivityServiceImpl extends ServiceImpl<TbSensitivityMapper, TbSensitivity>{

    @Autowired
    TbSensitivityMapper sensitivityMapper;

    @Autowired
    RestHighLevelClient restHighLevelClient;

    public void syncSensitivity() throws Exception {

        List<TbSensitivity> list = sensitivityMapper.selectList(new QueryWrapper<TbSensitivity>());

        BulkRequest request = new BulkRequest();

        for (int i = 0; i < list.size(); i++) {
            request.add(
                    new IndexRequest("min")
                            .id(list.get(i).getSensitivityId()+"")
                            .source(JSON.toJSONString(list.get(i)), XContentType.JSON)
            );
        }

        restHighLevelClient.bulk(request, RequestOptions.DEFAULT);

    }
}




